<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <!-- 引入组件库 -->
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <script src="../js/vue.js"></script>
        <script src="../plugins/elementui/index.js"></script>
        <script src="../js/axios-0.18.0.js"></script>
        <script src="../js/echarts.js"></script>
        <style>
            .el-date-editor--month:first-child {
                margin-left: 60%;
                margin-top: 5px;
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>统计分析<small>会员数量</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>统计分析</el-breadcrumb-item>
                    <el-breadcrumb-item>会员数量</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <el-date-picker
                            v-model="startMonth"
                            type="month"
                            @blur="changeMonth()"
                            value-format="yyyy-MM"
                            placeholder="起始月份">
                    </el-date-picker>
                    <el-date-picker
                            v-model="endMonth"
                            type="month"
                            value-format="yyyy-MM"
                            @blur="changeMonth()"
                            placeholder="结束月份">
                    </el-date-picker>
                    <!--{{startMonth}}{{endMonth}}-->
                    <!-- 为 ECharts 准备一个具备大小（宽高）的 DOM -->
                    <div id="chart1" style="height:600px;"></div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script>
        var vue = new Vue({
            el: "#app",
            data: {
                startMonth: "",
                endMonth: ""
            },
            mounted() {
                this.showMemberReport();
            },
            methods: {
                changeMonth() {
                    // TODO 校验所选的月份是一年以内
                    if (this.startMonth == null || this.startMonth == "") {
                        this.$message.warning("起始日期不能为空");
                        return;
                    }
                    if (this.endMonth == null || this.endMonth == "") {
                        this.$message.warning("结束日期不能为空");
                        return;
                    }
                    axios.get("/report/getMemberReportByMonthRange.do?startMonth=" + this.startMonth + "&endMonth=" + this.endMonth)
                        .then(res => {
                            if (res.data.flag) {
                                this.setOption(res);
                            } else {
                                this.$message.warning(res.data.message);
                            }
                        });
                },
                // 设置折线图数据
                setOption(res) {
                    var myChart1 = echarts.init(document.getElementById('chart1'));
                    myChart1.setOption(
                        {
                            title: {
                                text: '会员数量'
                            },
                            tooltip: {},
                            legend: {
                                data: ['会员数量']
                            },
                            xAxis: {
                                data: res.data.data.months
                            },
                            yAxis: {
                                type: 'value'
                            },
                            series: [{
                                name: '会员数量',
                                type: 'line',
                                data: res.data.data.memberCount
                            }]
                        });
                    this.startMonth = res.data.data.months[0];
                    this.endMonth = res.data.data.months[res.data.data.months.length - 1];
                },
                // 初始化折线图.默认展示当前月份前12个月的数据
                showMemberReport() {
                    axios.get("/report/getMemberReport.do").then((res) => {
                        this.setOption(res);
                    });
                },
            }
        });
    </script>
</html>
